﻿    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Demo.CommonBase.View.Forms;
using Demo.CommonBase.Logging;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Demo.Services.View.Controllers;
using Demo.Services.Contracts.DTO.Infos;
using Demo.Services.Contracts.DTO.Filters;
using Demo.CommonBase.DTO.Infos;
using Demo.Services.Services.Impl.Managers;

namespace Demo.Web.Operation
{
    public partial class OperationView : PageBase
    {
#region Properties

        public int ProyectId
        {
            get 
            {
                int id = 0;
                if (Session["ProyectId"] != null)
                {
                    id = (int)Session["ProyectId"];
                }
                return id;
            }
            set
            {
                Session["ProyectId"] = value;
            }
        }


#endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                ProyectId = string.IsNullOrEmpty(Request["Id"]) ? 0 : Convert.ToInt32(Request["Id"]);
                
                if (ProyectId != 0)
                {                    
                    GetOperation();
                }
                EnabledControls();
            }
            
        }
        
        protected void CboProyect_SelectChange(object sender, DirectEventArgs e)
        {
            OperationController controller;
            ProyectInfo proyect;
            int id ;
            try
            {
                Logger.Info();
                int.TryParse(cboProyect.SelectedItem.Value,out id);
                SetValue("ProyectId", id);
                controller = new OperationController();
                controller.ExecuteService(this, "Services.GetProyectById");
                proyect = (ProyectInfo)GetValue("ProyectInfo");
                

                if (proyect != null)
                {
                    txtHour.Text = proyect.Hours.ToString();
                    txtDateStar.Value = proyect.DateStart;
                    txtDateEnd.Value = proyect.DateEnd;
                    txtTecnology.Text = GetTecnologys(proyect.TecnologyList);
                }                
                
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

        }

        protected void grdResources_ReadData(object sender, StoreReadDataEventArgs e)
        {
            ResourceFilter filter;
            OperationController controller;
            ResourcePagingInfo resource;
            try
            {
                Logger.Info();
                filter = new ResourceFilter();
                filter.Id = ProyectId;

                filter.Pagination = new PaginationInfo();
                filter.Pagination.PageNum = e.Start;
                filter.Pagination.PageSize = e.Limit;

                SetValue("ResourceFilter", filter);
                controller = new OperationController();
                controller.ExecuteService(this, "Services.GetResourceByProyect");
                resource = (ResourcePagingInfo)GetValue("ResourcePagingInfo");

                if (resource != null)
                { 
                    e.Total =  resource.TotalReg;
                    stReosurce.DataSource = resource.Resources;
                    stReosurce.DataBind();
                }


            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
            

        private string GetTecnologys(IList<TecnologyInfo> tecnologies)
        {
            string result = string.Empty;
            try
            {
                if (tecnologies != null)
                {
                    for (int i = 0; i < tecnologies.Count; i++)
                    {
                        if (i == 0)
                        {
                            result = tecnologies[i].Description;
                        }
                        else
                        {
                            result = string.Format("{0},{1}",result,tecnologies[i].Description);
                        }

                    }
                }

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return result;
        }

        private void GetOperation()
        {
            OperationController controller;
            ProyectInfo proyect;
            try
            {
                Logger.Info();
                SetValue("ProyectId", ProyectId);
                controller = new OperationController();
                controller.ExecuteService(this, "Services.GetProyectById");
                proyect = (ProyectInfo)GetValue("ProyectInfo");

                if (proyect != null)
                {
                    txtHour.Text = proyect.Hours.ToString();
                    txtDateStar.Value = proyect.DateStart;
                    txtDateEnd.Value = proyect.DateEnd; 
                    txtTecnology.Text = GetTecnologys(proyect.TecnologyList);                    
                    cboProyect.SetValue(proyect.Name);
                    cboProyect.Disabled = true;
                } 
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        private void EnabledControls()
        {
            txtHour.Disabled = true;
            txtHour.Disabled = true;
            txtDateStar.Disabled = true;
            txtDateEnd.Disabled = true;
            txtTecnology.Disabled = true;
        }

        protected void BtnDeleteConfirm_Click(object sender, DirectEventArgs e)
        {
            try
            {
                Logger.Info();

                int id = 0;
                if (e.ExtraParams["id"] != null)
                {
                    id = Convert.ToInt32(e.ExtraParams["id"]);
                }

                X.Msg.Confirm("Confirmacion", "Desea Borrar Registro?", new MessageBoxButtonsConfig
                {
                    Yes = new MessageBoxButtonConfig
                    {
                        Handler = string.Format("App.direct.DoYes({0})", id),
                        Text = "Si"
                    },
                    No = new MessageBoxButtonConfig
                    {
                        Handler = "App.direct.DoNothing()",
                        Text = "No",
                    }
                }).Show();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

        }


        [DirectMethod]
        public void DoYes(int id)
        {
            OperationManager operation;
            try
            {
                operation = new OperationManager();
                operation.DeleteLineTime(id);
                Notification.Show(new NotificationConfig
                {
                    Title = "Información",
                    Icon = Icon.Information,
                    Html = "El registro fué borrado",
                    HideDelay = 3000
                });
                stReosurce.RemoveAll();
                stReosurce.Reload();
            }
            catch (Exception)
            {
                throw;
            }

        }

        [DirectMethod]
        public void DoNothing()
        {
            return;
        }

        [DirectMethod]
        public void msjconfirm()
        {
            X.MessageBox.Alert("info", "si jalo");
        }


        protected void BtnRefresh_Click(object sender, DirectEventArgs e)
        {
            try
            {
                Logger.Info();
                stReosurce.Reload();
                
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

        }

               
    }


    
}